//
//  NFPhotoBrowser.m
//  PhotoBrowser
//
//  Created by A_Dirt on 16/5/24.
//  Copyright © 2016年 程印. All rights reserved.
//

#define SCREEN_HEIGHT                   [[UIScreen mainScreen] bounds].size.height
//width of window when equipment vertical screen
#define SCREEN_WIDTH                    [[UIScreen mainScreen] bounds].size.width

#define Scaleheight ([[UIScreen mainScreen] bounds].size.height > 480 ? [[UIScreen mainScreen] bounds].size.height / 568 : [[UIScreen mainScreen] bounds].size.height / 480)

#define Scalewidth [[UIScreen mainScreen] bounds].size.width / 320

#import "NFPhotoBrowser.h"
#import "NFPhotoViewCell.h"
#import "NFDescribeView.h"
#import "UIView+SDAutoLayout.h"

@interface NFPhotoBrowser ()

@property(nonatomic, strong) UICollectionView *photoCollectionview;
//是否是PUSH进来
@property(nonatomic, assign) BOOL push;

@property(nonatomic, strong)  NFDescribeView     *describeView;;
@end

@implementation NFPhotoBrowser

//如果是PUSH进来
- (void)showViewWithPush:(UIViewController *)contor
{
    self.push = YES;
    [contor.navigationController pushViewController:self animated:YES];
}

//如果是模态弹出
- (void)showViewWithPresent:(UIViewController *)contor
{
   [contor presentViewController:self animated:YES completion:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];

    if ( [[[UIDevice currentDevice] systemVersion] floatValue] > 7.0) {
        // 判断是否是IOS7
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    }
    
    [self.navigationController setNavigationBarHidden:YES animated:TRUE];
    
    [self.navigationController setToolbarHidden:YES animated:TRUE];

}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    
    if ( [[[UIDevice currentDevice] systemVersion] floatValue] > 7.0) {
        // 判断是否是IOS7
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    }
    
    [self.navigationController setNavigationBarHidden:NO animated:TRUE];
    
    [self.navigationController setToolbarHidden:NO animated:TRUE];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor blackColor];
    
    [self setUpCollectionView];
    
    [self setUpTopView];
    
    [self setUpDescribeView];
}

- (void)setUpCollectionView
{
    //创建右边的collectionview
    UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    
    self.photoCollectionview = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH + 10, SCREEN_HEIGHT) collectionViewLayout:flowLayout];
    self.photoCollectionview.bounces = YES;
    self.photoCollectionview.delegate = self;
    self.photoCollectionview.dataSource = self;
    self.photoCollectionview.pagingEnabled = YES;
    self.photoCollectionview.showsHorizontalScrollIndicator = NO;
    self.photoCollectionview.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.photoCollectionview];
    
    //注册
    [self.photoCollectionview registerClass:[NFPhotoViewCell class]
              forCellWithReuseIdentifier:@"NFPhotoViewCell"];
  
    [self.photoCollectionview scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.currentIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    
}

- (void)setUpDescribeView
{
    self.describeView = [[NFDescribeView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT -140*Scalewidth , SCREEN_WIDTH, 140*Scalewidth)];
    [_describeView showViewWithinfo:_info index:self.currentIndex];
    [self.view addSubview:self.describeView];
    
}

//设置导航栏
- (void)setUpTopView
{
    UIView * topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    
    topView.backgroundColor = [UIColor clearColor];
    
    [self.view addSubview:topView];
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setFrame:CGRectMake(15, 25, 32, 32)];
    leftBtn.backgroundColor = [UIColor clearColor];
    [leftBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [topView addSubview:leftBtn];
    
    UIButton * rightBtn = [[UIButton alloc] init];
    rightBtn.backgroundColor = [UIColor redColor];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(heelStick) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [rightBtn setTitle:[NSString stringWithFormat:@"%@跟帖",self.number] forState:UIControlStateNormal];
    rightBtn.frame = CGRectMake(SCREEN_WIDTH - [self commentsHeight:rightBtn.titleLabel.text] - 15, 25, [self commentsHeight:rightBtn.titleLabel.text], 25);
    [rightBtn.layer setCornerRadius:10];
    [rightBtn.layer setMasksToBounds:YES];
    [topView addSubview:rightBtn];

}

- (void)buttonClick:(UIButton *)sender
{
    if (self.push) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else{
    
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)heelStick
{
    if (self.delegate) {
        [self.delegate touchHeelStick];
    }

}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return _info.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NFPhotoViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NFPhotoViewCell" forIndexPath:indexPath];
    [cell showViewInfo:_info indexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH,  self.photoCollectionview.bounds.size.height);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{

    return UIEdgeInsetsMake(5, 0, 5, 10);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

    CGFloat page = self.photoCollectionview.contentOffset.x/self.photoCollectionview.bounds.size.width;
    
    [_describeView showViewWithinfo:_info index:page];

}

- (CGFloat)commentsHeight: (NSString *)conten
{
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectZero];
    [lab setNumberOfLines:0];
    lab.font = [UIFont systemFontOfSize:14];
    lab.frame = CGRectMake(0, 21, SCREEN_WIDTH - 58, 0);
    lab.text = conten;
    [lab sizeToFit];
    
    return lab.bounds.size.width + 10;
    
}



















@end
